package com.ocom.webapi.controller;


import com.ocom.common.constant.WebApiServiceUrl;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.webapi.scaleupgrade.ScaleUpgradeAddRequest;
import com.ocom.common.vo.webapi.scaleupgrade.ScaleUpgradeModifyRequest;
import com.ocom.common.vo.webapi.scaleupgrade.ScaleUpgradeOnShowRequest;
import com.ocom.common.vo.webapi.scaleupgrade.ScaleUpgradePlanRequest;
import com.ocom.security.authentication.YoCiUser;
import com.ocom.security.utils.SecurityUtils;
import com.ocom.webapi.feign.PayConfigClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@PreAuthorize("@pms.hasRole('ROLE_WEB')")
@RestController
public class UpgradeController {

    @Autowired
    private PayConfigClientService payConfigClientService;

    @PostMapping(WebApiServiceUrl.Url_ScaleUpgradeAdd)
    public Result scaleUpgradeAdd(@RequestBody ScaleUpgradeAddRequest request) {
        YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
        if (yoCiUser.getRole() != 0l) {
            return ResultUtil.error(ResultEnum.Err_3028);
        }
        return payConfigClientService.newUpgradeCode(request);
    }


    @PostMapping(WebApiServiceUrl.Url_ScaleUpgradeModify)
    public Result scaleUpgradeModify(@RequestBody ScaleUpgradeModifyRequest request) {
        YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
        if (yoCiUser.getRole() != 0l) {
            return ResultUtil.error(ResultEnum.Err_3028);
        }
        return payConfigClientService.updateUpgradeCode(request);
    }


    @PostMapping(WebApiServiceUrl.Url_ScaleUpgradeOnShow)
    public Result scaleUpgradeOnShow(@RequestBody ScaleUpgradeOnShowRequest request) {
        YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
        if (yoCiUser.getRole() != 0l) {
            return ResultUtil.error(ResultEnum.Err_3028);
        }
        return payConfigClientService.getUpgradeCodeList(request);
    }


    @PostMapping(WebApiServiceUrl.Url_ScaleUpgradeStart)
    public Result scaleUpgradeStart(@RequestBody ScaleUpgradePlanRequest request) {
        YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
        if (yoCiUser.getRole() != 0l) {
            return ResultUtil.error(ResultEnum.Err_3028);
        }
        return payConfigClientService.startUpgradeCode(request);
    }


    @PostMapping(WebApiServiceUrl.Url_ScaleUpgradeFinish)
    public Result scaleUpgradeFinish(@RequestBody ScaleUpgradePlanRequest request) {
        YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
        if (yoCiUser.getRole() != 0l) {
            return ResultUtil.error(ResultEnum.Err_3028);
        }
        request.setStopMode(3);
        return payConfigClientService.stopUpgradeCode(request);
    }


    @PostMapping(WebApiServiceUrl.Url_ScaleUpgradePause)
    public Result scaleUpgradePause(@RequestBody ScaleUpgradePlanRequest request) {
        YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
        if (yoCiUser.getRole() != 0l) {
            return ResultUtil.error(ResultEnum.Err_3028);
        }
        request.setStopMode(2);
        return payConfigClientService.stopUpgradeCode(request);
    }


    @PostMapping(WebApiServiceUrl.Url_ScaleUpgradeUpgradePlan)
    public Result scaleUpgradeUpgradePlan(@RequestBody ScaleUpgradePlanRequest request) {
        YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
        if (yoCiUser.getRole() != 0l) {
            return ResultUtil.error(ResultEnum.Err_3028);
        }
        return payConfigClientService.getUpgradeCodePlan(request);
    }

    @PostMapping(WebApiServiceUrl.Url_ScaleUpgradeSetPlan)
    public Result scaleUpgradeSetPlan(@RequestBody ScaleUpgradePlanRequest request) {
        YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
        if (yoCiUser.getRole() != 0l) {
            return ResultUtil.error(ResultEnum.Err_3028);
        }
        if(request.getUpgradeCode().length()<8){
            return ResultUtil.error(ResultEnum.Err_4084);
        }
        return payConfigClientService.setUpgradeCodePlan(request);
    }

    @PostMapping(WebApiServiceUrl.Url_ScaleUpgradeGetScaleUpgradeStatus)
    public Result getScaleUpgradeStatus(@RequestBody ScaleUpgradeOnShowRequest request) {
        YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
        if (yoCiUser.getRole() != 0l) {
            return ResultUtil.error(ResultEnum.Err_3028);
        }
        return payConfigClientService.getScaleUpgradeStatus(request);
    }

}
